package com.danco.sn.model.hibernateutil;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
	
	private SessionFactory sessionFactory = null;
	private static HibernateUtil HIBERNATE_UTIL;
	
	private HibernateUtil(){
		sessionFactory=buildSessionFactory();
	}

	public static HibernateUtil getInstance(){
		if(HIBERNATE_UTIL==null){
			HIBERNATE_UTIL = new HibernateUtil();
		}
		
		return HIBERNATE_UTIL;
	}
	
	private SessionFactory buildSessionFactory() {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		return sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}
